/*
 *   Time Tracking Web Application
 *   Copyright (C) 2008, Nick Griffiths
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Contact nicobrevin at gmail dot com
 *   http://code.google.com/p/openharvest/
 */

package org.brevs.time.action.beans;

import org.brevs.time.manager.TimeManager;
import org.brevs.time.model.Entry;

public class EntryBean {
	
	public static EntryBean create(Entry entry) {
		EntryBean bean = new EntryBean();
		bean.id = entry.getId();
		bean.seconds = entry.getSeconds();
		bean.description = entry.getDescription();
		bean.categoryId = entry.getProjectCategory() == null 
			? null : entry.getProjectCategory().getId();
		bean.projectId = entry.getProject() == null
			? null : entry.getProject().getId();
		bean.count = formatSeconds(entry.getSeconds() + (entry.getRunningFor() / 1000));
		bean.running = entry.getStartedRunning() != null;
		bean.timerStarted = entry.isRunning() 
			? entry.getStartedRunning().getTime() : null;
		return bean;
	}

	private static String formatSeconds(long seconds) {
		long minutes = seconds / 60;
		long hours = minutes / 60;
		long remMinutes = minutes % 60;
		return Long.toString(hours) + ":" + (remMinutes > 9 ? "" : "0") + Long.toString(remMinutes);
	}

	private Long id;
	
	private String description;
	
	private Long projectId;
	
	private Long categoryId;
	
	private String count;
	
	private long seconds;

	private Long timerStarted;
	
	private boolean running;
	
	public Long getCategoryId() {
		return categoryId;
	}
	
	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}
	
	public String getCount() {
		return count;
	}
	
	public void setCount(String count) {
		this.count = count;
	}
	
	public String getDescription() {
		return description;
	}
	
	public long getSeconds() {
		return seconds;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getProjectId() {
		return projectId;
	}
	
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}
	
	public boolean isRunning() {
		return this.running;
	}
	
	public void setTimerStarted(Long timerStarted) {
		this.timerStarted = timerStarted;
	}
	
	public Long getTimerStarted() {
		return timerStarted;
	}
	
	public Entry toEntry(TimeManager timeManager) {
		Entry entry;
		if (this.id == null) {
			entry = new Entry(); 
		} else {
			entry = timeManager.getEntry(id);
		}
		
		entry.setDescription(description);
		
		// only apply seconds if it parses ok
		long seconds = convertCountToSeconds();
		if (seconds != -1 && !entry.isRunning()) {
			entry.setSeconds(seconds);
		}
		entry.setProject(projectId == null ? null : timeManager.getProject(projectId));
		entry.setProjectCategory(categoryId == null ? null : timeManager.getProjectCategory(categoryId));
		return entry;
	}

	private long convertCountToSeconds() {
		if (count != null && count.matches("[0-9]+(:[0-9]+)?")) {
			int idx;
			if ((idx = count.indexOf(":")) == -1) {
				return Integer.parseInt(count) * 60;
			} else {
				int hours = Integer.parseInt(count.substring(0, idx));
				int mins = Integer.parseInt(count.substring(idx + 1));
				return (((hours * 60) + mins) * 60);
			}
		} else {
			return -1;
		}
	}
}
